<template>
  <div
    class="login-container"
    :style="{
      background: 'url(' + require('@/images/bg@login.png') + ')',
      backgroundSize: 'cover'
    }"
  >
    <div class="navi_bar">
      <el-menu
        :default-active="$router.path"
        class="el-menu-demo"
        mode="horizontal"
        @select="handleSelect"
        background-color="#050505"
        text-color="#8c9abc"
        active-text-color="#ffffff"
      >
        <el-menu-item index="1">首页</el-menu-item>
        <img src="../../images/trademark.png" alt="" />
        <!-- <el-submenu index="2">
          <template slot="title"
            >我的工作台</template
          >
          <el-menu-item index="2-1">选项1</el-menu-item>
          <el-menu-item index="2-2">选项2</el-menu-item>
          <el-menu-item index="2-3">选项3</el-menu-item>
          <el-submenu index="2-4">
            <template slot="title"
              >选项4</template
            >
            <el-menu-item index="2-4-1">选项1</el-menu-item>
            <el-menu-item index="2-4-2">选项2</el-menu-item>
            <el-menu-item index="2-4-3">选项3</el-menu-item>
          </el-submenu>
        </el-submenu>
        <el-menu-item index="3" disabled>消息中心</el-menu-item>
        <el-menu-item index="4"
          ><a href="https://www.ele.me" target="_blank"
            >订单管理</a
          ></el-menu-item
        > -->
      </el-menu>
    </div>

    <div class="login-card">
      <a @click="temp()">
        <img src="../../images/login-code.png" alt="" />
      </a>
      <div class="login-card-left"></div>
      <div class="login-card-right">
        <div class="login-form">
          <div class="login_type" v-if="isCodeLogin">
            <a @click="loginByCode">验证码登录</a>
            <a style="color: #8c8c8c;" @click="loginByAc">账号登录</a>
          </div>
          <div class="login_type" v-if="isAcLogin">
            <a style="color: #8c8c8c;" @click="loginByCode">验证码登录</a>
            <a @click="loginByAc">账号登录</a>
          </div>
          <div class="login_info" v-if="isCodeLogin">
            <el-form
              class="phone-form"
              ref="form"
              :model="form"
              label-position="left"
              size="small"
              auto-complete="on"
              :rules="rules"
              v-if="!loggingStatus"
            >
              <el-form-item prop="phoneNum" :rules="rules.phoneNum" ref="phone">
                <el-input
                  size="small"
                  placeholder="手机号"
                  v-model="form.phoneNum"
                  class="input-with-select"
                  :validate-event="false"
                >
                  <el-select
                    v-model="select"
                    slot="prepend"
                    placeholder="请选择"
                    class="pt"
                  >
                    <el-option
                      v-for="item in phoneType"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                      <span style="float: left">{{ item.label }}</span>
                    </el-option>
                  </el-select>
                </el-input>
              </el-form-item>
              <el-form-item prop="checkCode">
                <el-input
                  class="check-code-box"
                  size="small"
                  v-model.number="form.checkCode"
                  placeholder="请输入验证码"
                  :validate-event="false"
                >
                  <el-button
                    :disabled="codeCd"
                    size="small"
                    slot="append"
                    @click="handleCaptcha('form')"
                    >获取验证码
                    <span v-if="codeCd">({{ long }})</span>
                  </el-button>
                </el-input>
              </el-form-item>
              <el-form-item>
                <el-button
                  class="custom-button"
                  @click="onSubmit('form')"
                  type="primary"
                  >登录</el-button
                >
              </el-form-item>
            </el-form>
          </div>
          <div class="login_info" v-if="isAcLogin">
            <el-form
              ref="loginForm"
              :model="loginForm"
              :rules="loginRules"
              class="login-form"
              label-position="left"
            >
              <el-form-item prop="username">
                <el-input
                  ref="username"
                  v-model="loginForm.username"
                  placeholder="账号"
                  name="username"
                  type="text"
                  tabindex="1"
                  auto-complete="on"
                  :validate-event="false"
                />
              </el-form-item>

              <el-form-item prop="password">
                <el-input
                  :key="passwordType"
                  ref="password"
                  v-model="loginForm.password"
                  :type="passwordType"
                  placeholder="密码"
                  name="password"
                  tabindex="2"
                  auto-complete="on"
                  @keyup.enter.native="handleLogin"
                  :validate-event="false"
                />
                <span class="show-pwd" @click="showPwd">
                  <svg-icon
                    :icon-class="
                      passwordType === 'password' ? 'eye' : 'eye-open'
                    "
                  />
                </span>
                <a class="toRegister" @click="gotoR">
                  还没有账号？去注册&nbsp;>
                </a>
              </el-form-item>
              <el-button
                :loading="loading"
                type="primary"
                style="width:100%;margin-bottom:30px;"
                @click.native.prevent="handleLogin"
                >登录</el-button
              >
            </el-form>
          </div>
        </div>
      </div>
    </div>
    <div class="footer">
      <span>浙ICP备2024077789号-1</span>
      <span>Copyright © 2024 DIYGEEK. All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";
import { login } from "@/api/user";

export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("Please enter the correct user name"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("The password can not be less than 6 digits"));
      } else {
        callback();
      }
    };
    const validatePhone = (rule, value, callback) => {
      // if (!value) {
      //   callback(new Error("手机号不能为空"));
      // }
      // // 使用正则表达式进行验证手机号码
      // if (!/^1[3456789]\d{9}$/.test(value)) {
      //   callback(new Error("手机号格式不正确"));
      // }
      // // 自定义校验规则 需要调用callback()函数！
      // callback();
      // console.log(value);
      // if (!value) {
      //   callback(new Error("手机号不能为空"));
      // } else if (!/^1[3456789]\d{9}$/.test(value)) {
      //   callback(new Error("手机号格式不正确"));
      // } else {
      //   callback();
      // }
    };
    return {
      loginForm: {
        username: "",
        password: ""
      },
      isCodeLogin: true,
      isAcLogin: false,
      activeIndex2: "1",
      loginRules: {
        username: [{ required: true, trigger: "blur" }],
        password: [
          { required: true, trigger: "blur", validator: validatePassword }
        ]
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
      // 登录状态
      loggingStatus: false,
      // 验证码是否CD
      codeCd: false,
      // CD长度
      long: 60,
      select: "+86",
      input: "",
      form: {
        phoneNum: null,
        checkCode: null
      },
      rules: {
        phoneNum: [
          { required: true, validator: validatePhone, trigger: "blur" }
        ],
        checkCode: [
          { required: true, message: "验证码不能为空哦！", trigger: "blur" }
        ]
      },
      phoneType: [
        {
          value: "中国",
          label: "+86"
        }
      ]
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    gotoR() {
      console.log(666);
      this.$router.push("/register");
    },
    handleSelect(key, keyPath) {
      console.log(key, keyPath);
    },
    temp() {
      this.$message({
        type: "error",
        message: "验证码登录都没有还想要有二维码登录啊,我看你真是饿了🤡",
        showClose: true,
        duration: 0
      });
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          this.loading = true;
          console.log(this.loginForm);
          let res = await login(this.loginForm);
          if (String(res.code) === "20000") {
            this.$store
              .dispatch("user/login", this.loginForm)
              .then(() => {
                this.$router.push({ path: this.redirect || "/" });
                this.loading = false;
                this.$store.commit("user/SET_NAME", this.loginForm.username);
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            console.log("error submit!!");
            return false;
          }
        }
      });
    },
    loginByCode() {
      this.isCodeLogin = true;
      this.isAcLogin = false;
    },
    loginByAc() {
      this.isCodeLogin = false;
      this.isAcLogin = true;
    },
    handleCaptcha(form) {
      this.$refs[form].validateField("phoneNum", async valid => {
        if (!valid) {
          this.$message({
            type: "error",
            message: "经费有限,请使用账号登录>_<",
            showClose: true,
            duration: 0
          });
          // 获取验证码
          //   captcha(this.form.phoneNum);
          //   // 开启计时
          //   this.codeCd = true;
          //   const timer = setInterval(() => {
          //     this.long--;
          //     if (this.long <= 0) {
          //       this.long = 60;
          //       this.codeCd = false;
          //       clearInterval(timer);
          //     }
          //   }, 1000);
          // } else {
          //   console.log("error submit!!");
          //   return false;
        }
      });
    }
  }
};
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #fff;
$cursor: #ffffff;

// @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
//   .login-container .el-input input {
//     color: $cursor;
//   }
// }

/* reset element-ui css */
.login-container {
  .el-input {
    .el-input-group__prepend {
      border: none;
      border-bottom: 1px solid #ebebeb;
      background-color: white;
    }
    .el-input-group__append {
      border: none;
      border-bottom: 1px solid #ebebeb;
      background-color: white;
      color: #007bff;
    }
    .el-input__inner {
      border: none;
      border-bottom: 1px solid #ebebeb;
      height: 4vh;
    }
    // display: inline-block;
    // height: 47px;
    // width: 85%;

    // input {
    //   background: transparent;
    //   border: 0px;
    //   -webkit-appearance: none;
    //   border-radius: 0px;
    //   padding: 12px 10px 12px 15px;
    // color: $light_gray;
    // height: 47px;
    // caret-color: $cursor;

    // &:-webkit-autofill {
    //   box-shadow: 0 0 0px 1000px $bg inset !important;
    //   -webkit-text-fill-color: $cursor !important;
    // }
    // }
  }

  // .el-form-item {
  //   // border: 1px solid rgba(255, 255, 255, 0.1);
  //   border: none;
  //   border-bottom: 1px solid #eeeeee;
  //   // background: rgba(0, 0, 0, 0.4);
  //   // border-radius: 5px;
  //   // color: #454545;
  // }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg !important;
  overflow: hidden;

  .navi_bar {
    img {
      position: absolute;
      right: 40px;
      bottom: 2px;
      width: 120px;
      height: 60px;
    }
  }

  .login-card {
    .toRegister {
      position: relative;
      left: 12vw;
      top: 1vh;
      color: #00aaff;
    }

    background-color: #ffffff;
    border: 1px solid #f9f9fa;
    position: absolute;
    top: 14vh;
    left: 20vw;
    display: flex;
    width: 60vw;
    height: 60vh;
    // box-shadow: 1px 1px 1px 1px #f6f6f7;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    img {
      width: 40px;
      height: 40px;
      position: absolute;
      right: 0;
    }

    .login-card-left {
      width: 50%;
      height: 100%;
      background-image: url("../../images/login@card.png");
      background-repeat: no-repeat;
      background-position: center;
      background-size: contain;
    }
    .login-card-right {
      width: 50%;
      .login-form {
        padding: 40px;
        .login_type {
          margin: 0px 20px 40px 30px;
          a {
            font-size: 20px;
          }
          a:nth-child(2) {
            margin-left: 8vw;
          }
        }
        .login_info {
          // ::v-deep .el-input__inner:nth-child(2) {
          //   border: none;
          //   border-bottom: 1px solid #ebebeb;
          // }
          ::v-deep .pt {
            width: 80px;
            padding: 0px;
            border: none;
            box-shadow: none;
          }
          .custom-button {
            margin-top: 5vh;
            width: 25vw;
            height: 4vh;
          }
          .show-pwd {
            position: relative;
            bottom: 45px;
            left: 280px;
          }
        }
      }
    }
  }

  .footer {
    // position: absolute;
    // bottom: 15vh;
    // left: 45vw;
    color: #767690;
    font-size: 14px;
    span:nth-child(2) {
      position: absolute;
      bottom: 12vh;
      left: 40vw;
    }
    span:nth-child(1) {
      position: absolute;
      bottom: 15vh;
      left: 45vw;
    }
  }
}
</style>
